import { defineStore } from 'pinia'
import {list} from '@/api/menu.ts'
import router from '@/router';
import {useRouteStore} from '@/stores/route.ts'
import {dictTypeList} from '@/api/dicttype.ts'
import {ref} from "vue";
const modules = import.meta.glob('@/views/**/*.vue')

export const useUserStore = defineStore('userStore', {
  // state: 返回对象的函数
  state: ()=>({
     // 登录token
     token: "",
     // 登录用户信息
     userInfo:{},
     expireTime:0,
	 menues:[],
	 dictList:[],
  }),
  getters: {},
  actions:{
	  async getDictionary(){
	  		  const {data} = await dictTypeList({})
	  		  this.dictList=data;
	  },
	  getDictData(dictType){
		  const dictDataList = ref([])
		  this.dictList.forEach((item)=>{
			  if (item.typeSign == dictType){
				  dictDataList.value = item.dictDataList;
				  return;
			  }
		  })
		  return dictDataList.value;
	  },
	  async setMenus(){
		  const {data} = await list({menuStatus:'1'});
		  
		  const menuRename: { id: any; parentId: any; path: string; name: any; redirect: any; meta: { title: any; icon: any }; component: () => Promise<any> }[]=[]
		  data.forEach((item)=>{
			if(item.menuType === '1' ||item.menuType === '0'){
				const obj = {
					id:item.id,
					parentId:item.parentId,
					path: '/'+item.menuRoute,
					param:item.routeParam,
					name: item.menuRoute,
					showStatus:item.showStatus,
					level:item.level,
					link:item.link,
					redirect:item.menuType == '0'?item.menuRoute:'',
					meta:{
						title: item.menuName,
						icon: item.menuIcon,
						isMenu:item.menuType === '1' ? true:false
					},
					component: item.menuType == '0'? modules[`/src/views/HomeView.vue`] : modules[`/src${item.menuPath}`],
				  }
				menuRename.push(obj)
			}
		  })
		  const menuList: never[] = []
		  menuRename.forEach((item)=>{
		  	item.children = this.getMenuChildren(item,menuRename)
		  	if(item.parentId == 0){
		  		menuList.push(item)
		  	}
		  })
		  menuList.forEach((item)=>{
			  this.addRouteMenu(item,item.children)
		  })
		  this.menues = menuList
		  const useRoute = useRouteStore()
		  useRoute.setRoutes(this.menues)
	  },
	  //循环树，添加路由
	  addRouteMenu(item,children){
	  		const childrenList = []
	  		children.forEach((child)=>{
				if(child.meta.isMenu){
				  if(child.param){
				  	childrenList.push({path: item.path+child.path+child.param,name:child.name,level:child.level,link:child.link,
				  		component: child.component,
				  		meta:{title:child.meta.title,icon:child.meta.icon,isMenu:child.meta.isMenu}}
				  		)
				  }else{
				  	childrenList.push({path: item.path+child.path,param:child.routeParam,name:child.name,level:child.level,link:child.link,
				  		component: child.component,
				  		meta:{title:child.meta.title,icon:child.meta.icon,isMenu:child.meta.isMenu}}
				  		)
				  }
				}
				if(child.children.length > 0){
					this.addRouteMenu(child,child.children)
				}
	  		})

	  		router.addRoute({ path: item.path,name:item.name,level:item.level,link:item.link,component: item.component,
	  		meta:{title:item.meta.title,isMenu:item.meta.isMenu},redirect:item.redirect,
	  		children:childrenList})
	  },
	  getMenuChildren(item,data){
	  	const children: any[] = []
	  	data.forEach((obj)=>{
	  		if(item.id == obj.parentId){
	  			children.push(obj)
	  			this.getMenuChildren(obj,data)
	  		}
	  	})
	  	return children;
	  },
      // 设置登录token
      setToken(token:string,expireTime:number){
          this.token = token;
          this.expireTime = expireTime;
      },
      // 设置登录用户信息
      setUserInfo(userInfo:any){
          this.userInfo = userInfo
      },
      clearToken() {
        this.token = '';
        this.expireTime = 0;
        this.userInfo = {};
      },
	  hasPermission(permissionCode){
		  let has  = false;
		  if(this.userInfo.roles){
			  this.userInfo.roles.forEach((role)=>{
			  			  if(role.permissions.includes(permissionCode)){
			  				 has = true;
			  			  }
			  })
		  }
		  return has;
	  }
  },
  persist: true
})

